package uk.co.BexhillWeb.MediaManager.Plugins.AlbumArt;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import uk.co.BexhillWeb.PluginApi.Abstract.WindowPanel;

@SuppressWarnings("serial")
public class AlbumArt extends WindowPanel implements Runnable {
	
	private ArrayList<SongItem> songItems = new ArrayList<SongItem>();
	private JLabel loadLbl = new JLabel("Album Art Manager v1.0");
	private JLabel loadLbl2 = new JLabel("");
	private JButton scanBtn = new JButton("Rescan Album Art");
	private JPanel cont = new JPanel();
	//loadLbl.setPreferredSize(new Dimension(this.getSize().width,20));
	//loadLbl.setMaximumSize(new Dimension(this.getSize().width,20));
	//loadLbl.setMinimumSize(new Dimension(this.getSize().width,20));
	//loadLbl2.setPreferredSize(new Dimension(this.getSize().width,20));
	//loadLbl2.setMaximumSize(new Dimension(this.getSize().width,20));
	//loadLbl2.setMinimumSize(new Dimension(this.getSize().width,20));
	
	public AlbumArt(String name) {
		super(name);
		//this.setBackground(Color.RED);
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill=GridBagConstraints.HORIZONTAL;
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.weightx=1;
		gbc.weighty=0;
		this.add(loadLbl,gbc);
		gbc.fill=GridBagConstraints.HORIZONTAL;
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.weightx=0;
		gbc.weighty=0;
		
		this.add(scanBtn,gbc);
		gbc.fill=GridBagConstraints.HORIZONTAL;
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.weightx=1;
		gbc.weighty=0;
		this.add(loadLbl2,gbc);
		gbc.fill=GridBagConstraints.BOTH;
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		gbc.weightx=1;
		gbc.weighty=1;
		cont.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.add(cont,gbc);
		
		scanBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				searchAlbumArt();
			}
			
		});
		//addMyEventListener(c);
		
		//for(int i=0; i<4; i++){
		//	songItems.add(at.getSongItem("Slipknot", "All Hope Is Gone"));
		//	
		//}
		//SongItem s = new SongItem("Slipknot", "All Hope Is Gone");
		//SongItem p = new SongItem("Slipknot", "All Hope Is Gone");
		//SongItem a = new SongItem("Slipknot", "All Hope Is Gone");
		//this.add(songItems.get(1));
		//this.add(a);
		//for each song in list, make new song item and add it to the list
		//generate ui for song item and add info to it
		//add the song item panels to the album art panel
		
	}

	public void searchAlbumArt() {
		Thread thread = new Thread(this);
		thread.start();
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0; i<2;i++){
			loadLbl2.setText("Loading album "+(i+1)+" of 2...");
			songItems.add(getSongItem("Slipknot","All Hope Is Gone"));
			cont.add(songItems.get(i));
			System.out.println("done!");
			//songItems.add(getSongItem("Slipknot","All Hope Is Gone"));
			this.invalidate();
			this.validate();
			this.repaint();
		}
		this.loadLbl2.setText("All album art loaded...");
		/**this.remove(cont);
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill=GridBagConstraints.BOTH;
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth=1;
		gbc.gridheight=1;
		this.add(cont,gbc);*/
		this.invalidate();
		this.validate();
		this.repaint();
		
		System.out.println("thread finished, found " + songItems.size() + " results.");
	}	
	private SongItem getSongItem(String artist, String album){
		return new SongItem(artist, album);
	}
	
}

